package com.aiju.widgit.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.aiju.base.R;
import com.aiju.base.dialog.BaseDialog;
import com.aiju.bean.Good;
import com.aiju.common.db.GoodService;
import com.aiju.utils.Util;
import com.aiju.widgit.MaxListView;
import com.aiju.widgit.ShoppingView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 佐为
 * @date: 2016-12-13 11:22
 * @version: V1.0
 * @Description: 购物车
 */
public class ShopCartDialog extends BaseDialog {

    /**
     * 商品总数量
     */
    private int googTotalSum;

    /**
     * 商品总价格
     */
    private double goodTotalPrice;

    /**
     * 商品列表
     */
    private MaxListView mListView;

    /**
     * 商品列表适配
     */
    private GoodAdapter mGoodAdapter;

    /**
     * 显示商品总数、总价
     */
    private TextView textViewTip, textViewNum;

    /**
     * 商品集合
     */
    private List<Good> goods = new ArrayList<>();

    /**
     * 商品数据库
     */
    private GoodService service;

    /**
     * 购物车商品变更回调
     */
    public interface OnCartChangeListner {
        void onCartChange();
    }

    /**
     * 默认构造器
     *
     * @param context
     */
    public ShopCartDialog(Context context) {
        super(context);
    }

    @Override
    protected int getDialogStyleId() {
        return R.style.LodingDialogStyle;
    }

    @Override
    protected View getView() {
        View root = mLayoutInflater.inflate(R.layout.dialog_shop, null);
        mListView = (MaxListView) root.findViewById(R.id.lv_shop_content);
        textViewTip = (TextView) root.findViewById(R.id.tv_biling_tip);
        textViewNum = (TextView) root.findViewById(R.id.tv_shop_money);
        root.findViewById(R.id.tv_shop_clear).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showClearDialog();
            }
        });
        service = new GoodService(mContext);
        return root;
    }

    /**
     * 设置对话框LayoutParams
     *
     * @return
     */
    public ShopCartDialog setLayoutParams() {
        getDialogWindow().setWindowAnimations(R.style.share_animation);
        getDialogWindow().setGravity(Gravity.BOTTOM);
        getDialogWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);//设置横向全屏
        return this;
    }

    /**
     * 是否购物车为空
     *
     * @return
     */
    public boolean isEmpty() {
        return Util.isEmpty(goods);
    }

    /**
     * 设置购物车底部总数，总价
     */
    private void setShopCartBotton() {
        googTotalSum = 0;
        goodTotalPrice = 0d;
        for (Good good : goods) {
            googTotalSum += good.getSelectSum();
            goodTotalPrice += good.getSelectPrice();
        }
        textViewTip.setText("" + googTotalSum);
        if (googTotalSum == 0) {
            textViewTip.setVisibility(View.GONE);
        } else {
            textViewTip.setVisibility(View.VISIBLE);
        }
        textViewNum.setText("" + goodTotalPrice);
        if (!Util.isEmpty(listners)) {
            for (OnCartChangeListner listner : listners) {
                listner.onCartChange();
            }
        }
    }

    /**
     * 显示清空购物车对话框
     */
    private void showClearDialog() {
        new SimpleDialog(mContext)
                .setLayoutParams()
                .setTitileText("是否清空购物车")
                .setOnSimpleClick(new SimpleDialog.OnSimpleClick() {
                    @Override
                    public void onCancel() {
                    }

                    @Override
                    public void onSubmit() {
                        for (Good g : goods) {
                            g.setSelectSum(0);
                            service.updateGood(g);
                        }

                        goods.clear();
                        mGoodAdapter.notifyDataSetChanged();
                        setShopCartBotton();
                        dismiss();
                    }
                })
                .setSystemAlert().show();
    }


    /**
     * 购物车列表适配器
     */
    private final class GoodAdapter extends BaseAdapter {
        private List<Good> shopEntities;

        public GoodAdapter(List<Good> shopEntities) {
            this.shopEntities = shopEntities;
        }

        @Override
        public int getCount() {
            return shopEntities.size();
        }

        @Override
        public Object getItem(int position) {
            return shopEntities.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_dialog_shop, parent, false);
            TextView name = (TextView) inflate.findViewById(R.id.tv_dialog_shop_name);
            TextView money = (TextView) inflate.findViewById(R.id.tv_dialog_shop_money);
            final ShoppingView shoppingView = (ShoppingView) inflate.findViewById(R.id.shopping_view);
            final Good good = shopEntities.get(position);
            if (good.isWeighGood()){
                shoppingView.setShowWeigh(good.getWeigh());
            }else {
                shoppingView.setSum(good.getSelectSum());
            }

            name.setText(good.getName());
            money.setText(good.getPrice());
            return inflate;
        }
    }

    private List<OnCartChangeListner> listners = new ArrayList<>();
    //==========get set==========

    public ShopCartDialog setOnCartChangeListner(OnCartChangeListner onCartChangeListner) {
        listners.add(onCartChangeListner);
        //this.onCartChangeListner = onCartChangeListner;
        return this;
    }

    public int getGoogTotalSum() {
        return googTotalSum;
    }

    public double getGoodTotalPrice() {
        return goodTotalPrice;
    }

    public List<Good> getGoods() {
        return goods;
    }
}
